//
//  FindViewController.m
//  QQ-Music
//
//  Created by lanou3g on 15/10/30.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "FindViewController.h"
#import "AppDelegate.h"
#import "HomeLabel.h"
#import "SingerAnd3DViewController.h"

@interface FindViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *titlesScrollView;
@property (weak, nonatomic) IBOutlet UIScrollView *contentsScrollView;

@end

@implementation FindViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发现";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 20, 18);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
    
    self.contentsScrollView.delegate = self;
    
    //不允许有重复的标题
//    self.titles = @[@"艺人|明星",@"3D体验馆",@"我是歌唱家"];
    
    // 0.添加子控制器
    [self setupChildVces];
    
    // 1.添加顶部的所有标题
    [self setupTitles];
    
    // 2.添加默认控制器
    UIViewController *firstVc = [self.childViewControllers firstObject];
    firstVc.view.frame = self.contentsScrollView.bounds;
    [self.contentsScrollView addSubview:firstVc.view];
    HomeLabel *firstLabel = [self.titlesScrollView.subviews firstObject];
    firstLabel.scale = 1.0;
    
    // 3.设置内容size
    CGFloat contentsContentW = self.childViewControllers.count * [UIScreen mainScreen].bounds.size.width;
    self.contentsScrollView.contentSize = CGSizeMake(contentsContentW, 0);
}


#pragma mark - Button点击事件
- (void) openOrCloseLeftList
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed)
    {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}

/**
 *  添加子控制器
 */
- (void)setupChildVces
{
    SingerAnd3DViewController *vc01 = [[SingerAnd3DViewController alloc] init];
    vc01.title = @"艺人|歌星";
    [self addChildViewController:vc01];
    
    SingerAnd3DViewController *vc02 = [[SingerAnd3DViewController alloc] init];
    vc02.title = @"3D奇妙体验馆";
    [self addChildViewController:vc02];
    
    UIViewController *vc03 = [[UIViewController alloc] init];
    vc03.title = @"我是歌唱家";
    [self addChildViewController:vc03];
    
}

/**
 *  添加顶部的所有标题
 */
- (void)setupTitles
{
    // 初始化标题
    NSUInteger count = self.childViewControllers.count;
    CGFloat labelW = 125;
    CGFloat labelH = 35;
    CGFloat labelY = 10;
    for (NSUInteger i = 0; i < count; ++i) {
        // 创建label
        HomeLabel *label = [[HomeLabel alloc] init];
        label.tag = i;
        [self.titlesScrollView addSubview:label];
        
        // 设置frame
        CGFloat labelX = i * labelW;
        label.frame = CGRectMake(labelX, labelY, labelW, labelH);
        
        // 设置文字
        UIViewController *vc = self.childViewControllers[i];
        label.text = vc.title;
        
        // 监听点击
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)]];
    }
    
    // 设置scrollView的内容大小
    CGFloat titlesContentW = count * labelW;
    self.titlesScrollView.contentSize = CGSizeMake(titlesContentW, 0);
}

/**
 *  监听label的点击
 */
- (void)labelClick:(UITapGestureRecognizer *)recognizer
{
    // 获得被点击的label
    HomeLabel *label = (HomeLabel *)recognizer.view;
    
    // 计算x方向上的偏移量
    CGFloat offsetX = label.tag * self.contentsScrollView.frame.size.width;
    
    // 设置偏移量
    CGPoint offset = CGPointMake(offsetX, self.contentsScrollView.contentOffset.y);
    [self.contentsScrollView setContentOffset:offset animated:YES];
}

#pragma mark - <UIScrollViewDelegate>
/**
 *  在scrollView动画结束时调用(添加子控制器的view到self.contentsScrollView)
 *  self.contentsScrollView == scrollView
 *  用户手动触发的动画结束，不会调用这个方法
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 获得当前需要显示的子控制器的索引
    NSUInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 滚动标题栏（self.titlesScrollView）
    HomeLabel *label = self.titlesScrollView.subviews[index];
    CGFloat titlesW = self.titlesScrollView.frame.size.width;
    CGFloat offsetX = label.center.x - titlesW * 0.5;
    // 最大的偏移量
    CGFloat maxOffsetX = self.titlesScrollView.contentSize.width - titlesW;
    if (offsetX < 0) {
        offsetX = 0;
    } else if (offsetX > maxOffsetX) {
        offsetX = maxOffsetX;
    }
    CGPoint offset = CGPointMake(offsetX, self.titlesScrollView.contentOffset.y);
    [self.titlesScrollView setContentOffset:offset animated:YES];
    
    // 让其它label
    [self.titlesScrollView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if (idx != index) {
            HomeLabel *otherLabel = self.titlesScrollView.subviews[idx];
            otherLabel.scale = 0.0;
        }
    }];
    
    UIViewController *vc = self.childViewControllers[index];
    // 如果子控制器的view已经在上面，就直接返回
    if (vc.view.superview) return;
    
    // 添加
    CGFloat vcW = scrollView.frame.size.width;
    CGFloat vcH = scrollView.frame.size.height;
    CGFloat vcX = index * vcW;
    CGFloat vcY = 0;
    vc.view.frame = CGRectMake(vcX, vcY, vcW, vcH);
    
    [scrollView addSubview:vc.view];
}

/**
 *  当scrollView停止滚动时调用这个方法（用户手动触发的动画停止，会调用这个方法）
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 1.需要进行控制的文字（2个文字）
    // 2.2个文字各自的比例值
    
    // 偏移量 / 宽度
#warning 这里最好取绝对值（保证计算出来的比例是非负数）
    CGFloat value = ABS(scrollView.contentOffset.x / scrollView.frame.size.width);
    
    // 左边文字的索引
    NSUInteger leftIndex = (NSUInteger)(scrollView.contentOffset.x / scrollView.frame.size.width);
    // 右边文字的索引
    NSUInteger rightIndex = leftIndex + 1;
    
    // 右边文字的比例
    CGFloat rightScale = value - leftIndex;
    // 左边文字的比例
    CGFloat leftScale = 1 - rightScale;
    
    // 取出label设置大小和颜色
    HomeLabel *leftLabel = self.titlesScrollView.subviews[leftIndex];
    leftLabel.scale = leftScale;
    if (rightIndex < self.titlesScrollView.subviews.count) {
        HomeLabel *rightLabel = self.titlesScrollView.subviews[rightIndex];
        rightLabel.scale = rightScale;
    }
}



@end
